package com.endbd.purchase.brand.brand.service.impl;

import com.endbd.feignapi.model.purchase.CBrand;
import com.endbd.feignapi.util.PageModel;
import com.endbd.purchase.manage.brand.dao.IBrandDao;
import com.endbd.purchase.manage.brand.model.BrandCondition;
import com.endbd.purchase.manage.brand.service.IBrandService;
import com.endbd.purchase.manage.type.dao.ITypeDao;
import com.endbd.purchase.manage.type.model.TypeCondition;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class BrandService implements IBrandService {

    @Autowired
    private IBrandDao brandDao;
    @Autowired
    private ITypeDao typeDao;


    @Override
    public PageModel<CBrand> queryBrandPageByCondition(PageModel<CBrand> pageModel, BrandCondition bcon) {


        int pageNum = pageModel.getPageNum()<=0?1:pageModel.getPageNum();
        int pageSize = pageModel.getPageSize()<=0?5:pageModel.getPageSize();

        PageHelper.startPage(pageNum,pageSize);
        List<CBrand> brands = brandDao.queryBrandCondition(bcon);

        PageInfo<CBrand> info = new PageInfo<CBrand>(brands);

        pageModel.setPageNum(info.getPageNum());
        pageModel.setPageSize(info.getPageSize());
        pageModel.setTotalPage(info.getPages());
        pageModel.setTotalCount((int)info.getTotal());
        pageModel.setData(brands);
        return pageModel;
    }

    @Override
    public void addBrand(CBrand brand) {
        brandDao.addBrand(brand);
    }

    @Override
    public void updateBrandById(CBrand brand) {
        brandDao.updateBrandById(brand);
    }

    @Override
    public void lorBrandById(BrandCondition bcon) {
        if (bcon.getBrandStatus()==1){
            brandDao.logBrand(bcon.getBrandId());
        }else {
            brandDao.recoveryBrand(bcon.getBrandId());
        }
    }
    
    @Override
    public List<String> querybrandName( BrandCondition bcon) {
        return brandDao.querybrandName(bcon);
    }

    @Override
    public Map<String,List<String>> brandType(){
        Map<String, List<String>> map = new HashMap<String, List<String>>();
        List<String> brandNames = brandDao.querybrandName(new BrandCondition());
        for (String brandName : brandNames) {
            TypeCondition tcon = new TypeCondition();
            tcon.setBrandName(brandName);
            List<String> typeName = typeDao.typeName(tcon);
            map.put(brandName,typeName);
        }
        return map;
    }
}
